1408A - Circle Coloring - CodeForces Solution


constructive algorithms *800

Please click on ads to support us..

Python Code:

for _ in [0]*int(input()):
 n=int(input())
 a=[*map(int,input().split())]
 b=[*map(int,input().split())]
 c=[*map(int,input().split())]
 p=[a[0]]
 for i in range(1,n):
  if p[-1]==a[i]:p.append(b[i])
  else:p.append(a[i])
 if p[0]==p[-1]:p[-1]=a[-1]
 if p[0]==p[-1] or p[-2]==p[-1]:p[-1]=b[-1]
 if p[0]==p[-1] or p[-2]==p[-1]:p[-1]=c[-1]
 print(*p)

C++ Code:

#include <bits/stdc++.h>
using namespace std;
 
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    int t,n;
    cin >> t;
    while(t--) {
        cin >> n;
        int a[3][n],prev=0;
        for(int i=0; i<3; ++i) for(int j=0; j<n; ++j) cin >> a[i][j];
        for(int i=0; i<n; ++i)
            for(int j=0; j<3; ++j)
                if(a[j][i]!=prev && (i<n-1 || i==n-1 && a[j][i]!=a[0][0])) {
                    prev=a[j][i];
                    cout << prev << ' ';
                    break;
                }
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

1711D - Rain
534A - Exam
1472A - Cards for Friends
315A - Sereja and Bottles
1697C - awoo's Favorite Problem
165A - Supercentral Point
1493A - Anti-knapsack
1493B - Planet Lapituletti
747B - Mammoth's Genome Decoding
1591C - Minimize Distance
1182B - Plus from Picture
1674B - Dictionary
1426C - Increase and Copy
520C - DNA Alignment
767A - Snacktower
1365A - Matrix Game
714B - Filya and Homework
31A - Worms Evolution
1691A - Beat The Odds
433B - Kuriyama Mirai's Stones
892A - Greed
32A - Reconnaissance
1236D - Alice and the Doll
1207B - Square Filling
1676D - X-Sum
1679A - AvtoBus
1549A - Gregor and Cryptography
918C - The Monster
4B - Before an Exam
545B - Equidistant String